<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="设备品牌" prop="brand">
                <el-input v-model="queryParams.brand" placeholder="请输入品牌" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="产品系列" prop="series">
                            <el-input
                                    v-model="queryParams.series"
                                    placeholder="请输入产品系列"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item> -->
            <el-form-item label="设备型号" prop="model">
                <el-input v-model="queryParams.model" placeholder="请输入设备型号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="内部型号" prop="internalModel">
                            <el-input
                                    v-model="queryParams.internalModel"
                                    placeholder="请输入内部型号"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item> -->
            <el-form-item label="设备编号" prop="deviceCode">
                <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="飞控序列号" prop="flightControllerSn">
                            <el-input
                                    v-model="queryParams.flightControllerSn"
                                    placeholder="请输入飞控序列号"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item> -->
            <!-- <el-form-item label="动力方式" prop="powerType">
                            <el-select v-model="queryParams.powerType" placeholder="请选择动力方式" clearable>
                                <el-option
                                        v-for="dict in power_mode"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                />
                            </el-select>
                        </el-form-item> -->
            <!-- <el-form-item label="轴距" prop="wheelbase">
                            <el-input
                                    v-model="queryParams.wheelbase"
                                    placeholder="请输入轴距"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="无人机类型" prop="droneType">
                            <el-select v-model="queryParams.droneType" placeholder="请选择无人机类型" clearable>
                                <el-option
                                        v-for="dict in drone_type"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="最大飞行时间" prop="maxFlightTime">
                            <el-input
                                    v-model="queryParams.maxFlightTime"
                                    placeholder="请输入最大飞行时间"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="防护等级" prop="protectionRating">
                            <el-input
                                    v-model="queryParams.protectionRating"
                                    placeholder="请输入防护等级"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="关联相机型号" prop="linkedCamera">
                            <el-input
                                    v-model="queryParams.linkedCamera"
                                    placeholder="请输入关联相机型号"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="最大水平飞行速度" prop="maxHorizontalSpeed">
                            <el-input
                                    v-model="queryParams.maxHorizontalSpeed"
                                    placeholder="请输入最大水平飞行速度"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="最大上升速度" prop="maxAscentSpeed">
                            <el-input
                                    v-model="queryParams.maxAscentSpeed"
                                    placeholder="请输入最大上升速度"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="最大下降速度" prop="maxDescentSpeed">
                            <el-input
                                    v-model="queryParams.maxDescentSpeed"
                                    placeholder="请输入最大下降速度"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="最大可承受风速" prop="maxWindResistance">
                            <el-input
                                    v-model="queryParams.maxWindResistance"
                                    placeholder="请输入最大可承受风速"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="最大载重" prop="maxPayload">
                            <el-input
                                    v-model="queryParams.maxPayload"
                                    placeholder="请输入最大载重"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="最小起飞重量" prop="minTakeoffWeight">
                            <el-input
                                    v-model="queryParams.minTakeoffWeight"
                                    placeholder="请输入最小起飞重量"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="旋翼数量" prop="rotorCount">
                            <el-input
                                    v-model="queryParams.rotorCount"
                                    placeholder="请输入旋翼数量"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item> -->
            <el-form-item label="产品序列号" prop="productSn">
                <el-input v-model="queryParams.productSn" placeholder="请输入产品序列号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="所属组织ID" prop="orgId">
                            <el-input
                                    v-model="queryParams.orgId"
                                    placeholder="请输入所属组织ID"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item> -->
            <!-- <el-form-item label="固件版本" prop="firmwareVersion">
                            <el-input
                                    v-model="queryParams.firmwareVersion"
                                    placeholder="请输入固件版本"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="民航登记号" prop="aviationRegistration">
                            <el-input
                                    v-model="queryParams.aviationRegistration"
                                    placeholder="请输入民航登记号"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="最后在线时间" prop="lastOnlineTime">
                            <el-date-picker clearable
                                            v-model="queryParams.lastOnlineTime"
                                            type="date"
                                            value-format="YYYY-MM-DD"
                                            placeholder="请选择最后在线时间">
                            </el-date-picker>
                        </el-form-item> -->
            <el-form-item label="设备状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 197px">
                    <el-option v-for="dict in drone_status" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="绑定状态" prop="bindStatus">
                            <el-select v-model="queryParams.bindStatus" placeholder="请选择绑定状态" clearable>
                                <el-option
                                        v-for="dict in drone_bind_status"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                />
                            </el-select>
                        </el-form-item> -->
            <!-- <el-form-item label="绑定的机场ID" prop="boundAirportId">
                            <el-input
                                    v-model="queryParams.boundAirportId"
                                    placeholder="请输入绑定的机场ID"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="删除标记" prop="isDelete">
                            <el-input
                                    v-model="queryParams.isDelete"
                                    placeholder="请输入删除标记"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['equipment:drone_info:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['equipment:drone_info:edit']">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['equipment:drone_info:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['equipment:drone_info:export']">导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="drone_infoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="无人机唯一ID" align="center" prop="id"/> -->
            <el-table-column label="无人机编号" align="center" prop="deviceCode" />
            <el-table-column label="无人机名称" align="center" prop="droneName" />
            <el-table-column label="产品序列号" align="center" prop="productSn" />
            <el-table-column label="品牌" align="center" prop="brand" />
            <!-- <el-table-column label="产品系列" align="center" prop="series"/> -->
            <el-table-column label="型号" align="center" prop="model" />
            <!-- <el-table-column label="内部型号" align="center" prop="internalModel"/> -->

            <!-- <el-table-column label="飞控序列号" align="center" prop="flightControllerSn"/> -->
            <!-- <el-table-column label="动力方式" align="center" prop="powerType">
                        <template #default="scope">
                                <dict-tag :options="power_mode" :value="scope.row.powerType"/>
                        </template>
</el-table-column>
<el-table-column label="轴距" align="center" prop="wheelbase" />
<el-table-column label="无人机类型" align="center" prop="droneType">
    <template #default="scope">
                                <dict-tag :options="drone_type" :value="scope.row.droneType"/>
                        </template>
</el-table-column> -->
            <!-- <el-table-column label="最大飞行时间" align="center" prop="maxFlightTime"/>
                    <el-table-column label="防护等级" align="center" prop="protectionRating"/>
                    <el-table-column label="关联相机型号" align="center" prop="linkedCamera"/>
                    <el-table-column label="最大水平飞行速度" align="center" prop="maxHorizontalSpeed"/>
                    <el-table-column label="最大上升速度" align="center" prop="maxAscentSpeed"/>
                    <el-table-column label="最大下降速度" align="center" prop="maxDescentSpeed"/>
                    <el-table-column label="最大可承受风速" align="center" prop="maxWindResistance"/>
                    <el-table-column label="最大载重" align="center" prop="maxPayload"/>
                    <el-table-column label="最小起飞重量" align="center" prop="minTakeoffWeight"/>
                    <el-table-column label="旋翼数量" align="center" prop="rotorCount"/> -->

            <el-table-column label="所属组织ID" align="center" prop="orgId" />
            <!-- <el-table-column label="固件版本" align="center" prop="firmwareVersion"/>
                    <el-table-column label="民航登记号" align="center" prop="aviationRegistration"/> -->
            <el-table-column label="最后在线时间" align="center" prop="lastOnlineTime" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.lastOnlineTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="状态" align="center" prop="status">
                        <template #default="scope">
                                <dict-tag :options="drone_status" :value="scope.row.status"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="绑定状态" align="center" prop="bindStatus">
                        <template #default="scope">
                                <dict-tag :options="drone_bind_status" :value="scope.row.bindStatus"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="绑定的机场ID" align="center" prop="boundAirportId"/>
                    <el-table-column label="图片" align="center" prop="imageUrl" width="100">
                        <template #default="scope">
                            <image-preview :src="scope.row.imageUrl" :width="50" :height="50"/>
                        </template>
                    </el-table-column> -->
            <!-- <el-table-column label="删除标记" align="center" prop="isDelete"/>
                    <el-table-column label="备注" align="center" prop="remark"/> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleDetails(scope.row)"
                        v-hasPermi="['equipment:dock_info:query']">详情
                    </el-button>
                    <el-button link type="primary" icon="Edit" @click="handleUnbind(scope.row)"
                        v-hasPermi="['equipment:drone_info:edit']">解绑
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改无人机信息对话框 -->
        <el-dialog :title="title" v-model="open" width="800px" append-to-body>
            <el-form ref="drone_infoRef" :model="form" :rules="rules" label-width="135px">
                <el-row>
                    <el-col :span="formSpan">
                        <el-form-item label="无人机名称" prop="droneName">
                            <el-input v-model="form.droneName" placeholder="请输无人机名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="品牌" prop="brand">
                            <el-input v-model="form.brand" placeholder="请输入品牌" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="产品系列" prop="series">
                            <el-input v-model="form.series" placeholder="请输入产品系列" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="产品型号" prop="model">
                            <el-input v-model="form.model" placeholder="请输入产品型号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="内部型号" prop="internalModel">
                            <el-input v-model="form.internalModel" placeholder="请输入内部型号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="设备编号" prop="deviceCode">
                            <el-input v-model="form.deviceCode" placeholder="请输入设备编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="飞控序列号" prop="flightControllerSn">
                            <el-input v-model="form.flightControllerSn" placeholder="请输入飞控序列号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="动力方式" prop="powerType">
                            <el-select v-model="form.powerType" placeholder="请选择动力方式">
                                <el-option v-for="dict in power_mode" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="轴距" prop="wheelbase">
                            <el-input v-model="form.wheelbase" placeholder="请输入轴距" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="无人机类型" prop="droneType">
                            <el-select v-model="form.droneType" placeholder="请选择无人机类型">
                                <el-option v-for="dict in drone_type" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="最大飞行时间" prop="maxFlightTime">
                            <el-input v-model="form.maxFlightTime" placeholder="请输入最大飞行时间" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="防护等级" prop="protectionRating">
                            <el-input v-model="form.protectionRating" placeholder="请输入防护等级" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="关联相机型号" prop="linkedCamera">
                            <el-input v-model="form.linkedCamera" placeholder="请输入关联相机型号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="最大水平飞行速度" prop="maxHorizontalSpeed">
                            <el-input v-model="form.maxHorizontalSpeed" placeholder="请输入最大水平飞行速度" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="最大上升速度" prop="maxAscentSpeed">
                            <el-input v-model="form.maxAscentSpeed" placeholder="请输入最大上升速度" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="最大下降速度" prop="maxDescentSpeed">
                            <el-input v-model="form.maxDescentSpeed" placeholder="请输入最大下降速度" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="最大可承受风速" prop="maxWindResistance">
                            <el-input v-model="form.maxWindResistance" placeholder="请输入最大可承受风速" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="最大载重" prop="maxPayload">
                            <el-input v-model="form.maxPayload" placeholder="请输入最大载重" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="最小起飞重量" prop="minTakeoffWeight">
                            <el-input v-model="form.minTakeoffWeight" placeholder="请输入最小起飞重量" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="旋翼数量" prop="rotorCount">
                            <el-input v-model="form.rotorCount" placeholder="请输入旋翼数量" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="产品序列号" prop="productSn">
                            <el-input v-model="form.productSn" placeholder="请输入产品序列号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="所属组织ID" prop="orgId">
                            <el-input v-model="form.orgId" placeholder="请输入所属组织ID" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="固件版本" prop="firmwareVersion">
                            <el-input v-model="form.firmwareVersion" placeholder="请输入固件版本" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="民航登记号" prop="aviationRegistration">
                            <el-input v-model="form.aviationRegistration" placeholder="请输入民航登记号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="最后在线时间" prop="lastOnlineTime">
                            <el-date-picker clearable v-model="form.lastOnlineTime" type="date"
                                value-format="YYYY-MM-DD" placeholder="请选择最后在线时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="formSpan">
                        <el-form-item label="状态" prop="status">
                            <el-select v-model="form.status" placeholder="请选择状态">
                                <el-option v-for="dict in drone_status" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="formSpan">
                        <el-form-item label="绑定状态" prop="bindStatus">
                            <el-select v-model="form.bindStatus" placeholder="请选择绑定状态">
                                <el-option v-for="dict in drone_bind_status" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="formSpan">
                        <el-form-item label="绑定的机场" prop="boundAirportId">
                            <el-select v-model="form.boundAirportId" placeholder="请选择机场" filterable clearable
                                loading-text="加载中..." :loading="airportLoading">
                                <el-option v-for="item in unboundAirports" :key="item.id" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-form-item label="图片" prop="imageUrl">
                        <image-upload v-model="form.imageUrl" :limit="1" />
                    </el-form-item>
                    <!-- <el-col :span="formSpan">
                                <el-form-item label="删除标记" prop="isDelete">
                                    <el-input v-model="form.isDelete" placeholder="请输入删除标记"/>
                                </el-form-item>
                            </el-col> -->
                    <el-col :span="formSpan">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Drone_info">
import {
    listDrone_info,
    getDrone_info,
    delDrone_info,
    addDrone_info,
    updateDrone_info,
} from "@/api/equipment/drone_info";

import { listAllNoBind } from '@/api/equipment/dock_info';

const { proxy } = getCurrentInstance();
const { power_mode, drone_status, drone_bind_status, drone_type } = proxy.useDict('power_mode', 'drone_status', 'drone_bind_status', 'drone_type');

const drone_infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const formSpan = ref(12);

const router = useRouter();

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        droneName: null,
        brand: null,
        series: null,
        model: null,
        internalModel: null,
        deviceCode: null,
        flightControllerSn: null,
        powerType: null,
        wheelbase: null,
        droneType: null,
        maxFlightTime: null,
        protectionRating: null,
        linkedCamera: null,
        maxHorizontalSpeed: null,
        maxAscentSpeed: null,
        maxDescentSpeed: null,
        maxWindResistance: null,
        maxPayload: null,
        minTakeoffWeight: null,
        rotorCount: null,
        productSn: null,
        orgId: null,
        firmwareVersion: null,
        aviationRegistration: null,
        lastOnlineTime: null,
        status: null,
        bindStatus: null,
        boundAirportId: null,
        imageUrl: null,
        isDelete: null,
    },
    rules: {
        droneName: [
            {
                required: true, message: "无人机名称不能为空", trigger: "blur"
            }
        ],
        brand: [
            {
                required: true, message: "品牌不能为空", trigger: "blur"
            }
        ],
        series: [
            {
                required: true, message: "产品系列不能为空", trigger: "blur"
            }
        ],
        model: [
            {
                required: true, message: "产品型号不能为空", trigger: "blur"
            }
        ],
        internalModel: [
            {
                required: true, message: "内部型号不能为空", trigger: "blur"
            }
        ],
        deviceCode: [
            {
                required: true, message: "设备编号不能为空", trigger: "blur"
            }
        ],
        flightControllerSn: [
            {
                required: true, message: "飞控序列号不能为空", trigger: "blur"
            }
        ],
        powerType: [
            {
                required: true, message: "动力方式不能为空", trigger: "change"
            }
        ],
        wheelbase: [
            {
                required: true, message: "轴距不能为空", trigger: "blur"
            }
        ],
        droneType: [
            {
                required: true, message: "无人机类型不能为空", trigger: "change"
            }
        ],
        maxFlightTime: [
            {
                required: true, message: "最大飞行时间不能为空", trigger: "blur"
            }
        ],
        protectionRating: [
            {
                required: true, message: "防护等级不能为空", trigger: "blur"
            }
        ],
        linkedCamera: [
            {
                required: true, message: "关联相机型号不能为空", trigger: "blur"
            }
        ],
        maxHorizontalSpeed: [
            {
                required: true, message: "最大水平飞行速度不能为空", trigger: "blur"
            }
        ],
        maxAscentSpeed: [
            {
                required: true, message: "最大上升速度不能为空", trigger: "blur"
            }
        ],
        maxDescentSpeed: [
            {
                required: true, message: "最大下降速度不能为空", trigger: "blur"
            }
        ],
        maxWindResistance: [
            {
                required: true, message: "最大可承受风速不能为空", trigger: "blur"
            }
        ],
        maxPayload: [
            {
                required: true, message: "最大载重不能为空", trigger: "blur"
            }
        ],
        minTakeoffWeight: [
            {
                required: true, message: "最小起飞重量不能为空", trigger: "blur"
            }
        ],
        rotorCount: [
            {
                required: true, message: "旋翼数量不能为空", trigger: "blur"
            }
        ],
        productSn: [
            {
                required: true, message: "产品序列号不能为空", trigger: "blur"
            }
        ],
        orgId: [
            {
                required: true, message: "所属组织ID不能为空", trigger: "blur"
            }
        ],
        firmwareVersion: [
            {
                required: true, message: "固件版本不能为空", trigger: "blur"
            }
        ],
        aviationRegistration: [
            {
                required: true, message: "民航登记号不能为空", trigger: "blur"
            }
        ],
        lastOnlineTime: [
            {
                required: true, message: "最后在线时间不能为空", trigger: "blur"
            }
        ],
        status: [
            {
                required: true, message: "状态不能为空", trigger: "change"
            }
        ],
        bindStatus: [
            {
                required: true, message: "绑定状态不能为空", trigger: "change"
            }
        ],
        boundAirportId: [
            {
                required: true, message: "绑定的机场ID不能为空", trigger: "blur"
            }
        ],
        imageUrl: [
            {
                required: true, message: "图片不能为空", trigger: "blur"
            }
        ],
        isDelete: [
            {
                required: true, message: "删除标记不能为空", trigger: "blur"
            }
        ],
    }
})
    ;

const { queryParams, form, rules } = toRefs(data);

/** 查询无人机信息列表 */
function getList() {
    loading.value = true;
    listDrone_info(queryParams.value).then(response => {
        drone_infoList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        droneName: null,
        brand: null,
        series: null,
        model: null,
        internalModel: null,
        deviceCode: null,
        flightControllerSn: null,
        powerType: null,
        wheelbase: null,
        droneType: null,
        maxFlightTime: null,
        protectionRating: null,
        linkedCamera: null,
        maxHorizontalSpeed: null,
        maxAscentSpeed: null,
        maxDescentSpeed: null,
        maxWindResistance: null,
        maxPayload: null,
        minTakeoffWeight: null,
        rotorCount: null,
        productSn: null,
        orgId: null,
        firmwareVersion: null,
        aviationRegistration: null,
        lastOnlineTime: null,
        status: null,
        bindStatus: null,
        boundAirportId: null,
        imageUrl: null,
        isDelete: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
    };
    proxy.resetForm("drone_infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加无人机信息";
    getUnboundAirports()
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getDrone_info(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改无人机信息";
    });
    getUnboundAirports()
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["drone_infoRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateDrone_info(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            }
            else {
                addDrone_info(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    }
    );
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除无人机信息编号为"' + _ids + '"的数据项？').then(function () {
        return delDrone_info(_ids);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}



/** 导出按钮操作 */
function handleExport() {
    proxy.download('equipment/drone_info/export', {
        ...queryParams.value
    }, `drone_info_${new Date().getTime()}.xlsx`)
}

/** 详情按钮操作 */
function handleDetails(row) {
        reset();
        const _id = row.id || ids.value
        router.push({ path: "/equipment/equipment/equipment/droneView",query: {selected: row.id}})
    }

getList();
/** 解绑按钮 */
function handleUnbind(row) {
    proxy.$modal.confirm('是否确认解绑无人机信息编号为"' + row.id + '"的数据项？').then(function () {
        return updateDrone_info({
            id: row.id,
            bindStatus: 0,
            boundAirportId: 0
        });
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("解绑成功");
    }).catch(() => { });
}


// 机场选择相关
const unboundAirports = ref([]);
const airportLoading = ref(false)

const getUnboundAirports = async () => {
    try {
        airportLoading.value = true
        const res = await listAllNoBind();
        unboundAirports.value = res || [];
    } catch (error) {
        console.error('获取机场列表失败:', error)
        proxy.$modal.msgError("机场列表加载失败")
    } finally {
        airportLoading.value = false
    }
}

</script>
